package com.yq.cp.controller.permission;


import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.permission.User;
import com.yq.cp.entity.permission.vo.UserVo;
import com.yq.cp.service.basics.OrganizationalStructureService;
import com.yq.cp.service.basics.PositionService;
import com.yq.cp.service.permission.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh
 * @since 2022-09-21
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    OrganizationalStructureService organizationalStructureService;
    @Resource
    PositionService positionService;
    @PostMapping("add")
    public ApiResult add(@RequestBody UserVo vo){
        vo.setCreateUserId(UserThreadLocal.get().getId());
        userService.add(vo);
        return ApiResult.ok();
    }

    @PostMapping("update")
    public ApiResult update(@RequestBody UserVo vo){
        vo.setCreateUserId(UserThreadLocal.get().getId());

        userService.update(vo);
        return ApiResult.ok();
    }
    @PostMapping("updateState")
    public ApiResult updateState(@RequestBody UserVo vo){
        userService.updateState(vo);
        return ApiResult.ok();
    }

    @PostMapping("delete")
    public ApiResult delete(@RequestBody UserVo vo) {
        vo.setCreateUserId(UserThreadLocal.get().getId());
        userService.delete(vo);
        return ApiResult.ok();
    }

    /**
     * 个人信息
     * @return
     */
    @PostMapping("personalInformation")
    public ApiResult personalInformation() {
        User user = UserThreadLocal.get();
        if (Objects.isNull(user.getStructureId())&&Objects.isNull(user.getSiteId()))
            user.setDepartmentName("超级管理员");
        if (!Objects.isNull(user.getStructureId())){
            OrganizationalStructure byId = organizationalStructureService.getById(user.getStructureId());
            if (byId.getType()==1)
                user.setDepartmentName("渠道商负责人");
            if (byId.getType()==2)
                user.setDepartmentName("大区负责人");
            if (byId.getType()==3)
                user.setDepartmentName("省级负责人");
            if (byId.getType()==4)
                user.setDepartmentName("市级负责人");
            if (byId.getType()==5)
                user.setDepartmentName("区级负责人");
            if (byId.getType()==6)
                user.setDepartmentName("站点负责人");

        }
        if (!Objects.isNull(user.getPositionId())){
            Position byId = positionService.getById(user.getPositionId());
            user.setDepartmentName(byId.getName());
        }
        return ApiResult.success(user);
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("updatePassword")
    public ApiResult updatePassword(@RequestBody UserVo userVo) {
        userService.updatePassword(userVo);
        return ApiResult.success();
    }

    /**
     * 添加openid
     * @param user
     * @return
     */
    @PostMapping("addOpenId")
    public ApiResult addOpenId(@RequestBody User user) {
        User byId = userService.getById(user);
        if (!Objects.isNull(byId.getOpenId()))
            return ApiResult.success();
        userService.saveOrUpdate(user);
        return ApiResult.success();
    }
}

